package org.cars.db.service;

import io.quarkus.hibernate.orm.PersistenceUnit;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.cars.db.entity.second_db.City;

import java.util.List;

@ApplicationScoped
public class CityService {
    @Inject
    @PersistenceUnit("second_db")
    EntityManager entityManager;

    public City getSingle(Integer id) {
        return entityManager.find(City.class, id);
    }

    public List<City> get() {
        return entityManager.createNamedQuery("City.findAll", City.class)
                .getResultList();
    }

    @Transactional
    public void create(City fruit) {
        entityManager.persist(fruit);
    }

    @Transactional
    public void update(Integer id, City fruit) {
        City entity = entityManager.find(City.class, id);

        if (null != entity) {
            entity.setName(fruit.getName());
        }
    }

    @Transactional
    public void delete(Integer id) {
        City entity = entityManager.getReference(City.class, id);

        if (null != entity) {
            entityManager.remove(entity);
        }
    }
}